﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LaughingDog.GameObjects;
using Microsoft.Xna.Framework;
using FarseerGames.FarseerPhysics.Factories;
using LaughingDog.Managers;

namespace LaughingDog.Components
{
    public class RevoluteJoint : Joint
    {      
        private RigidBody mConnectedBody = null;
        private FarseerGames.FarseerPhysics.Dynamics.Joints.RevoluteJoint mRevoluteJoint = null;

        /// <summary>
        /// The second RigidBody this Spring is attached to. The first RigidBody is attached to the GameObject that the Joint is attached to.
        /// </summary>
        public RigidBody ConnectedBody { get { return mConnectedBody; } set { mConnectedBody = value;} }

        /// <summary>
        /// Gets or sets the the anchor of the joint in world coordinates.
        /// </summary>
        public Vector2 Anchor { get { return mRevoluteJoint.Anchor; } set { mRevoluteJoint.Anchor = value; } }

        /// <summary>
        /// Gets the world position of the joint after simulation has started.
        /// </summary>
        public Vector2 CurrentAnchor { get { return mRevoluteJoint.CurrentAnchor;} }

        /// <summary>
        /// Creates an instance of a RevoluteJoint component.
        /// </summary>
        /// <param name="gameObject">The GameObject this component is attached to.</param>
        public RevoluteJoint(GameObject gameObject)
            : base(gameObject)
        {
            mRevoluteJoint = new FarseerGames.FarseerPhysics.Dynamics.Joints.RevoluteJoint();
            mJoint = mRevoluteJoint;           
        }

        /// <summary>
        /// Create the Joint after setting initial settings such as the ConnectedBody.
        /// </summary>
        public override void Create()
        {
            mRevoluteJoint = JointFactory.Instance.CreateRevoluteJoint(PhysicsManager.Instance.Simulator, GameObject.RigidBody.Body, mConnectedBody.Body, mInitialAnchor);
            mJoint = mRevoluteJoint;
        }
    }
}
